/*
 * @Author: your name
 * @Date: 2020-04-20 10:07:29
 * @LastEditTime: 2020-07-14 12:01:38
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: /tbl-app/lib/cells/eventbus.dart
 */ 
import 'package:synchronized/synchronized.dart';
typedef void EventCallback(arg);
class EventBus {
  //私有构造函数
  EventBus._internal();

  //保存单例
  static EventBus _singleton = new EventBus._internal();

  //工厂构造函数
  factory EventBus()=> _singleton;

  //保存事件订阅者队列，key:事件名(id)，value: 对应事件的订阅者队列
  var _emap = new Map<Object, List<EventCallback>?>();

  //添加订阅者
  void on(eventName, EventCallback? f) {
    if (eventName == null || f == null) return;
    _emap[eventName] ??= <EventCallback>[];
    _emap[eventName]!.add(f);
  }

  //移除订阅者
  void off(eventName, [EventCallback? f]) {
    var list = _emap[eventName];
    if (eventName == null || list == null) return;
    if (f == null) {
      _emap[eventName] = null;
    } else {
      list.remove(f);
    }
  }


  //触发事件，事件触发后该事件所有订阅者会被调用
  void emit(eventName, [arg])async {
    var lock = Lock();
    if (!lock.locked) {
      await lock.synchronized(() async {
        var list = _emap[eventName];
        if (list == null) return;
        int len = list.length - 1;
        //反向遍历，防止在订阅者在回调中移除自身带来的下标错位
        for (var i = len; i > -1; --i) {
          list[i](arg);
        }
      });
    }

  }
}